package com.wancaijinfu.component;

import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.TypedValue;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.wancaijinfu.actitvity.R;
import com.wancaijinfu.file.FileCache;
import com.wancaijinfu.file.StoryPath;

public class MImageLoader {
	DisplayImageOptions options;
	static MImageLoader imageLoader;
	ImageLoaderConfiguration config = null;
	Context context;

	public static enum OPATIONS {
		HEAD, PIC
	}

	@SuppressWarnings("deprecation")
	public MImageLoader(Context context) {
		this.context = context;
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.user_head)
				.showImageForEmptyUri(R.drawable.user_head)
				.showImageOnFail(R.drawable.user_head).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true)
				.displayer(new FadeInBitmapDisplayer(300))
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		File file = new File(StoryPath.getCacheDir(context,
				FileCache.IMAGE_LODER));
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context).discCache(new UnlimitedDiskCache(file)).build();
		ImageLoader.getInstance().init(config);
	}

	public static MImageLoader getInstance(Context context) {
		if (imageLoader == null) {
			imageLoader = new MImageLoader(context);
		}
		return imageLoader;
	}

	public void showBitmap(String url, ImageView imageView) {
		ImageLoader.getInstance().displayImage(url, imageView, options);
	}

	// public void showBitmap(String url, ImageView imageView, int dp) {
	// ImageLoader.getInstance().displayImage(url, imageView, options);
	// }
	//
	// public void showPicBitmap(String url, ImageView imageView, int dp) {
	// ImageLoader.getInstance().displayImage(url, imageView, options);
	// }

	public MImageLoader setOptions(OPATIONS option, int dp) {
		int value = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources()
						.getDisplayMetrics());
		if (option == OPATIONS.HEAD) {
			if (dp <= 0) {
				options = new DisplayImageOptions.Builder()
						.showImageOnLoading(R.drawable.user_head)
						.showImageForEmptyUri(R.drawable.user_head)
						.showImageOnFail(R.drawable.user_head)
						.cacheInMemory(true).cacheOnDisk(true)
						.considerExifParams(true)
						.displayer(new FadeInBitmapDisplayer(300))
						.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
						.bitmapConfig(Bitmap.Config.RGB_565).build();
			} else
				options = new DisplayImageOptions.Builder()
						.showImageOnLoading(R.drawable.profile_no_avarta_icon)
						.showImageForEmptyUri(R.drawable.profile_no_avarta_icon)
						.showImageOnFail(R.drawable.profile_no_avarta_icon)
						.cacheInMemory(true).cacheOnDisk(true)
						.considerExifParams(true)
						.displayer(new RoundedBitmapDisplayer(value))
						.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
						.bitmapConfig(Bitmap.Config.RGB_565).build();
		} else if (option == OPATIONS.PIC) {
			if (dp <= 0)
				options = new DisplayImageOptions.Builder()
						.showImageOnLoading(R.drawable.ic_empty)
						.showImageForEmptyUri(R.drawable.ic_empty)
						.showImageOnFail(R.drawable.ic_empty)
						.cacheInMemory(true).cacheOnDisk(true)
						.considerExifParams(true)
						.displayer(new FadeInBitmapDisplayer(300))
						.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
						.bitmapConfig(Bitmap.Config.RGB_565).build();
			else
				options = new DisplayImageOptions.Builder()
						.showImageOnLoading(R.drawable.ic_empty)
						.showImageForEmptyUri(R.drawable.ic_empty)
						.showImageOnFail(R.drawable.ic_empty)
						.cacheInMemory(true).cacheOnDisk(true)
						.considerExifParams(true)
						.displayer(new RoundedBitmapDisplayer(value))
						.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
						.bitmapConfig(Bitmap.Config.RGB_565).build();
		}
		return this;
	}

	public MImageLoader setOptions(int dp, int loadingRes, int emptyRes,
			int failRes) {
		int value = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources()
						.getDisplayMetrics());
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(loadingRes)
				.showImageForEmptyUri(emptyRes)
				.showImageOnFail(failRes)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.considerExifParams(true)
				.displayer(
						dp < 0 ? new FadeInBitmapDisplayer(300)
								: new RoundedBitmapDisplayer(value))
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		return this;
	}

	public MImageLoader setLoadingListener(ImageLoadingListener listener) {
		ImageLoader.getInstance().setDefaultLoadingListener(listener);
		return this;
	}

	public static void clearCache() {
		ImageLoader.getInstance().clearMemoryCache();
		ImageLoader.getInstance().clearDiskCache();
	}
}
